﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Controls.SalesManSVC;
using SMS.Common.Silverlight;
using SMS.Entities;

namespace SMS.Model
{
    public class SalesManModel
    {
        ServiceProxy<SalesManServiceClient> proxy = null;
        public event Action<int> OnSaveSalesManCompleted;
        public event Action<System.Collections.ObjectModel.ObservableCollection<SalesManDetail>> OnSearchCompleted;
        public event Action<SMSException> OnServiceError;
        public SalesManModel()
        {
            proxy = new ServiceProxy<SalesManServiceClient>("Service/SalesManService.svc");
            proxy.Client.SaveSalesManCompleted += new EventHandler<SaveSalesManCompletedEventArgs>(Client_SaveSalesManCompleted);
            proxy.Client.SearchSalesManCompleted += new EventHandler<SearchSalesManCompletedEventArgs>(Client_SearchSalesManCompleted);
        }

        void Client_SearchSalesManCompleted(object sender, SearchSalesManCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSearchCompleted != null)
                    OnSearchCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_SaveSalesManCompleted(object sender, SaveSalesManCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnSaveSalesManCompleted != null)
                    OnSaveSalesManCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        public void SaveSalesMan(SalesManDetail objSalesManDetail)
        {
            proxy.Client.SaveSalesManAsync(App.LoggedinUserName, objSalesManDetail);
        }
        public void SearchSalesMan(string searchText)
        {
            proxy.Client.SearchSalesManAsync(App.LoggedinUserName, searchText);
        }
    }
}
